package pers.hl.module_main.common

import android.content.Context
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.yanzhenjie.recyclerview.SwipeMenuCreator
import com.yanzhenjie.recyclerview.SwipeMenuItem
import pers.hl.module_main.R

/**
 * Rv侧滑菜单帮助类
 */
object SwipeMenuHelper {

    fun createDeleteItem(context: Context): SwipeMenuCreator {
        return createRightItem(context, "删除")
    }

    /**
     * 创建单个侧滑菜单
     */
    fun createRightItem(context: Context, singleName: String): SwipeMenuCreator {
        return SwipeMenuCreator { leftMenu, rightMenu, position ->
            val leftItem = SwipeMenuItem(context)
            setMenuItemStyle(context, singleName, leftItem)
            rightMenu.addMenuItem(leftItem)
        }
    }

    /**
     * 创建左侧侧滑菜单
     */
    fun createLeftItem(context: Context, vararg menuName: String): SwipeMenuCreator {
        return SwipeMenuCreator { leftMenu, _, _ ->
            val leftItem = SwipeMenuItem(context)
            for (name in menuName) {
                setMenuItemStyle(context, name, leftItem)
                leftMenu.addMenuItem(leftItem)
            }
        }
    }

    /**
     * 创建右侧侧滑菜单
     */
    fun createRightItem(context: Context, vararg menuName: String): SwipeMenuCreator {
        return SwipeMenuCreator { _, rightMenu, _ ->
            for (name in menuName) {
                val item = SwipeMenuItem(context)
                setMenuItemStyle(context, name, item)
                rightMenu.addMenuItem(item)
            }
        }
    }

    private fun setMenuItemStyle(context: Context, name: String, item: SwipeMenuItem) {
        item.text = name
        item.setTextColor(
            ContextCompat.getColor(context, R.color.white)
        )
        item.setBackgroundColor(
            ContextCompat.getColor(context, R.color.red)
        )
        item.height = LinearLayout.LayoutParams.MATCH_PARENT
        item.width = 150
        item.textSize = 18
    }
}